package com.example.runningman.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.runningman.R;

import com.example.runningman.bean.Img;

import com.example.runningman.util.SPUtil;

import java.text.SimpleDateFormat;
import java.util.List;


import androidx.recyclerview.widget.RecyclerView;

public class Img_adapter extends RecyclerView.Adapter<Img_adapter.ViewHolder> {
    List<Img> mData;
    Activity mActivity;
    RequestOptions options;
    String useid;
    boolean f =false;
    public Img_adapter(List<Img> data, Activity activity) {
        this.mData = data;

        this.mActivity = activity;
        useid= SPUtil.getInstance(mActivity).getString("useid");
        options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .override(80,80)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.NONE);
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;

        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_img, parent, false);



        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Log.e("sss", mData.get(position).getImgurl());
        Glide.with(mActivity).load(mData.get(position).getImgurl()).into(holder.mPic);
        holder.mPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dia;
                dia = new Dialog(mActivity, R.style.edit_AlertDialog_style);
                dia.setContentView(R.layout.activity_start_dialog);
                ImageView imageView = (ImageView) dia.findViewById(R.id.start_img);
                Log.e("sss", mData.get(position).getImgurl());
                Glide.with(mActivity).load(mData.get(position).getImgurl()).into(imageView);
                //选择true的话点击其他地方可以使dialog消失，为false的话不会消失
                dia.setCanceledOnTouchOutside(true); // Sets whether this dialog is
                Window w = dia.getWindow();
                WindowManager.LayoutParams lp = w.getAttributes();
                lp.x = 0;
                lp.y = 40;
                dia.onWindowAttributesChanged(lp);
                dia.show();
                imageView.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dia.dismiss();
                            }
                        });

            }
        });

    }



    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void refresh(List<Img> data){
        this.mData = data;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mPic;


        public ViewHolder(View itemView) {
            super(itemView);

            mPic = itemView.findViewById(R.id.img_art);



        }
    }


}
